﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management; 

namespace MSysMonNET
{
    public class MSysCheckDisks
    {
        /// <summary>
        /// returns the space available on the system disk, in bytes
        /// </summary>
        /// <param name="scope">the remote server's management scope</param>
        /// <param name="targetDisk">the disk to analyze</param>
        /// <returns>the disk's free space, in bytes</returns>
        public ulong checkDiskSpace(ManagementScope scope, string targetDisk)
        {
            ulong result = 0;

            try
            {

                //Query system for Operating System information
                ObjectQuery query = new ObjectQuery(
                    "SELECT * FROM Win32_LogicalDisk where deviceid='" + targetDisk + "'");
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher(scope, query);

                ManagementObjectCollection queryCollection = searcher.Get();

                foreach (ManagementObject objDisk in queryCollection)
                {
                    result = (ulong)objDisk["FreeSpace"];
                }
            }
            catch 
            {
                return 0;
            }

            return result;
        }

    }
}
